package com.lsyw.intergrate.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsyw.intergrate.common.Result;
import com.lsyw.intergrate.entity.UnopDept;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.lsyw.intergrate.service.UnopDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2023-04-18
 */
@Api(tags = "部门表")
@RestController
@RequestMapping("/unop-dept")
public class UnopDeptController {


    @Autowired
    private UnopDeptService unopDeptService;


    /**
     * 详情
     */
    @GetMapping("/get-by-id")
    @ApiOperation(value = "详情", notes = "详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门id", required = true, paramType = "query", dataType = "String")
    })
    public Result getById(String id) {
        return Result.successResult(unopDeptService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST")
    public Result save(@RequestBody UnopDept unopDept) {
        unopDept.setId(IdUtil.simpleUUID());
        unopDept.setIsBf(0);
        unopDept.setDelFlag(0);
        unopDept.setGmtCreate(new Date());
        unopDeptService.save(unopDept);
        return Result.successResult(null);
    }

    /**
     * 设置被服使用部门
     */
    @GetMapping("/update/isBf")
    @ApiOperation(value = "设置被服使用部门", notes = "设置被服使用部门", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门id", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isBf", value = "1取消设置 2 设置成使用部门", required = false, paramType = "query", dataType = "Integer")
    })
    public Result delete(String id, Integer isBf) {
        UnopDept dept = this.unopDeptService.getById(id);
        dept.setIsBf(isBf);
        this.unopDeptService.updateById(dept);
        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id")
    @ApiOperation(value = "删除部门", notes = "删除部门", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部门id", required = false, paramType = "query", dataType = "String")
    })
    public Result delete(String id) {
        UnopDept dept = this.unopDeptService.getById(id);
        dept.setDelFlag(1);
        this.unopDeptService.updateById(dept);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "修改", httpMethod = "POST")
    public Result updateById(@RequestBody UnopDept unopDept) {
        unopDept.setGmtModified(new Date());
        unopDeptService.updateById(unopDept);
        return Result.successResult(null);
    }


    /**
     * 查询列表（不分页）
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询列表（不分页）", notes = "查询列表（不分页）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "查询条件", required = false, paramType = "query", dataType = "String")
    })
    public Result list(String name) {
        final LambdaQueryWrapper<UnopDept> lambda = new QueryWrapper<UnopDept>().lambda();
        if (!StringUtils.isEmpty(name)) {
            lambda.like(UnopDept::getName, "%" + name + "%");
        }
        lambda.eq(UnopDept::getIsBf, 2);
        unopDeptService.list(lambda);
        return Result.successResult(unopDeptService.list(lambda));
    }


    /**
     * 分页查询
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询(部门维护)", notes = "分页查询", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "condition:hospitalArea", value = "园区", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:name", value = "名称", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "condition:isBf", value = "是否为被服使用科室", required = false, paramType = "query", dataType = "String")
    })
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<UnopDept> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        LambdaQueryWrapper<UnopDept> lambda = new QueryWrapper<UnopDept>().lambda();
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("hospitalArea") != null && !StringUtils.isEmpty(condition.get("hospitalArea").toString())) {
            lambda.eq(UnopDept::getHospitalArea, condition.get("hospitalArea"));
        }
        if (condition.get("name") != null && !StringUtils.isEmpty(condition.get("name").toString())) {
            lambda.like(UnopDept::getName, "%" + condition.get("name").toString() + "%");
        }
        if (condition.get("isBf") != null && !StringUtils.isEmpty(condition.get("isBf").toString())) {
            lambda.like(UnopDept::getIsBf, "%" + condition.get("isBf").toString() + "%");
        }
        lambda.eq(UnopDept::getDelFlag, 0);
        Page<UnopDept> page1 = this.unopDeptService.page(page, lambda);
        return Result.successResult(page1);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<UnopDept> lambda, UnopDeptReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(UnopDept::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getParentId())) {
            lambda.eq(UnopDept::getParentId, param.getParentId());
        }
        if (!StringUtils.isEmpty(param.getAncestors())) {
            lambda.eq(UnopDept::getAncestors, param.getAncestors());
        }
        if (!StringUtils.isEmpty(param.getCode())) {
            lambda.eq(UnopDept::getCode, param.getCode());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            lambda.eq(UnopDept::getName, param.getName());
        }
        if (!StringUtils.isEmpty(param.getFullName())) {
            lambda.eq(UnopDept::getFullName, param.getFullName());
        }
        if (!StringUtils.isEmpty(param.getSort())) {
            lambda.eq(UnopDept::getSort, param.getSort());
        }
        if (!StringUtils.isEmpty(param.getHospitalArea())) {
            lambda.eq(UnopDept::getHospitalArea, param.getHospitalArea());
        }
        if (!StringUtils.isEmpty(param.getBranchCode())) {
            lambda.eq(UnopDept::getBranchCode, param.getBranchCode());
        }
        if (!StringUtils.isEmpty(param.getModuleType())) {
            lambda.eq(UnopDept::getModuleType, param.getModuleType());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(UnopDept::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(UnopDept::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(UnopDept::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(UnopDept::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(UnopDept::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(UnopDept::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(UnopDept::getGmtModified, param.getGmtModifiedList().get(1));
        }
        if (!StringUtils.isEmpty(param.getDelFlag())) {
            lambda.eq(UnopDept::getDelFlag, param.getDelFlag());
        }
        if (!StringUtils.isEmpty(param.getFinanceCode())) {
            lambda.eq(UnopDept::getFinanceCode, param.getFinanceCode());
        }
        lambda.orderBy(true, false, UnopDept::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class UnopDeptReqVo extends UnopDept {
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
